package com.coda;

import com.coda.properties.JWTProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestController;

import java.rmi.registry.Registry;
import java.util.Arrays;

@SpringBootApplication
public class AdminApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }

    @Autowired
    private ApplicationContext context;

    // 替换成你自己项目的包名（前缀）
    private static final String BASE_PACKAGE = "com.coda"; // ⬅️ 修改为你的实际项目包路径

    @Override
    public void run(ApplicationArguments args) {
        System.out.println("======= 项目中的自定义 Bean（Controller、Service、Mapper、Handler 等） =======");

        String[] allBeanNames = context.getBeanDefinitionNames();
        Arrays.sort(allBeanNames);

        int count = 0;
        for (String beanName : allBeanNames) {
            Class<?> beanType = context.getType(beanName);
            if (beanType == null) continue;

            String className = beanType.getName();

            // 条件 1：包名前缀匹配（项目中代码）
            boolean isProjectBean = className.startsWith(BASE_PACKAGE);

            // 条件 2：包含目标注解（也适用于部分自定义情况）
            boolean hasTargetAnnotation = AnnotationUtils.findAnnotation(beanType, Component.class) != null
                    || AnnotationUtils.findAnnotation(beanType, Controller.class) != null
                    || AnnotationUtils.findAnnotation(beanType, RestController.class) != null
                    || AnnotationUtils.findAnnotation(beanType, Service.class) != null
                    || AnnotationUtils.findAnnotation(beanType, ControllerAdvice.class) != null;

            if (isProjectBean && hasTargetAnnotation) {
                System.out.println(beanName + " -> " + className);
                count++;
            }
        }

        System.out.println("======= 共找到 " + count + " 个项目自定义 Bean =======");
    }

}
